
goog.provide('esn.client.StudentClient');

goog.require('esn.client.BaseClient');


/**
 * @constructor
 */
esn.client.StudentClient = function(xhrManager) {
  goog.base(this, esn.client.StudentClient.SERVICE_NAME, xhrManager);
};
goog.inherits(esn.client.StudentClient, esn.client.BaseClient);

esn.client.StudentClient.SERVICE_NAME = 'studentservice';

esn.client.StudentClient.Method = {
  GET_STUDENTS: 'getStudents',
  SAVE_FACEBOOK_FRIENDS: 'saveFacebookFriends',
  SAVE_FACEBOOK_PROFILE: 'saveFacebookProfile',
  SAVE_STUDENT_CONSENT: 'saveStudentConsent',
  SAVE_STUDENT_STATUS: 'saveStudentStatus'
};

esn.client.StudentClient.prototype.getStudents = function(opt_callback) {
  var request = {};
  this.send(
      esn.client.StudentClient.Method.GET_STUDENTS,
      request,
      goog.bind(this.getStudentsResponse_, this, opt_callback || null));
};

esn.client.StudentClient.prototype.getStudentsResponse_ = function(callback,
    responseJson) {
  var response = new esn.client.Response();
  if ('error' in responseJson) {
    response.setError(responseJson['error']);
  } else {
    var students = goog.array.map(responseJson['students'], function(student) {
      return new esn.model.StudentModel(student);
    });
    response.setData(students);
  }

  if (callback) {
    callback(response);
  }
};

esn.client.StudentClient.prototype.saveFacebookProfile = function(profile,
    opt_callback) {
  var request = {
    'profile': profile
  };
  this.send(
      esn.client.StudentClient.Method.SAVE_FACEBOOK_PROFILE,
      request,
      opt_callback);
};

esn.client.StudentClient.prototype.saveFacebookFriends = function(fb_friends,
    fb_matches, opt_callback) {
  var request = {
    'fb_friends': fb_friends,
    'fb_matches': fb_matches    
  };
  this.send(
      esn.client.StudentClient.Method.SAVE_FACEBOOK_FRIENDS,
      request,
      opt_callback);
};

esn.client.StudentClient.prototype.saveStudentConsent = function(consented,
    opt_callback) {
  var request = {'consented': consented};
  this.send(
      esn.client.StudentClient.Method.SAVE_STUDENT_CONSENT,
      request,
      opt_callback);
};

/**
 * @param status The status should be the last completed section id for the
 *     logged in user.
 */
esn.client.StudentClient.prototype.saveStudentStatus = function(status,
    opt_callback) {
  var request = {'status': status};
  this.send(
      esn.client.StudentClient.Method.SAVE_STUDENT_STATUS,
      request,
      opt_callback);
};
